<?php

class Listener_Order_Payed_Email extends Listener_Base
{
    public function handle($args)
    {
        $self       = new self();
        $self->args = $args;
        //判断队列是否开启,如果开启则把任务推送到队列
        if ($self->queue_status) {
            //推送到队列执行
            Queue::in('default', Listener_Order_Payed_Email::class, $args);
        } else {
            $self->perform();
        }

    }

    public function perform()
    {
        $args       = $this->args;
        $ordersn    = $args['ordersn'];
        $pay_source = $args['pay_source'] ?? '';

        $order = DB::select()
            ->from('member_order')
            ->where('ordersn', '=', $ordersn)
            ->execute()
            ->current();

        switch ($order['need_confirm']) {
            case 0://无需确认
                $type = St_NoticeCommon::PRODUCT_ORDER_PAYSUCCESS_MSGTAG;
                break;
            case 1://支付后确认
                if ($pay_source == '后台') {
                    $type = St_NoticeCommon:: PRODUCT_ORDER_CONFIRMED;
                } else {
                    $type = St_NoticeCommon::PRODUCT_ORDER_UNPROCESSING_MSGTAG;
                }
                break;
            case 2://支付前确认
                $type = St_NoticeCommon::PRODUCT_ORDER_PAYSUCCESS_MSGTAG;
                break;
            default:
                $type = '';
                break;
        }
        if ($type) {
            $result = St_EmailService::send_product_order_email($type, $order);
            St_Log::write('邮件通知', array('type' => $type, 'ordersn' => $ordersn, 'result' => $result), 'event/order_payed/log');
        }
    }

}